package com.lsege.controller.backstage;

import com.github.pagehelper.PageHelper;
import com.lsege.entity.JsonResult;
import com.lsege.entity.Page;
import com.lsege.entity.backstage.Bike;
import com.lsege.entity.backstage.VoBike;
import com.lsege.service.backstage.BikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by lsd on 2017-06-26.
 */
@RestController
@RequestMapping("/bike")
public class BikeController {
    @Autowired
    BikeService bikeService;

    /**
     * 添加单车
     * @param bike
     * @return
     */
    @PostMapping("addBike")
    public JsonResult addBike(Bike bike){
        String block_UUID = UUID.randomUUID().toString().replaceAll("-", "");
        bike.setBlockUUID(block_UUID);
        JsonResult json = new JsonResult();
        Long num = bikeService.selectBikeCode(bike.getBicycleCode());
        if (num!=0){
            json.setSuccess(false);
            json.setMessage("该车已存在");
        }else {
            try {
                bikeService.addBikeInfo(bike);
                json.setSuccess(true);
                json.setMessage("添加成功");
            }catch (Exception e){
                e.printStackTrace();
                json.setSuccess(false);
                json.setMessage("添加失败");
            }
        }
        return json;
    }

    /**
     * 批量添加单车
     * @param file
     * @return
     */
    @PostMapping("addBatchBike")
    public JsonResult addBatchBike(MultipartFile file){
        JsonResult json=new JsonResult();
        try {
            String dependency=bikeService.addBatchBike(file);
            if (dependency.equals("")){
                json.setSuccess(true);
                json.setMessage("添加成功");
            }else {
                json.setSuccess(false);
                json.setMessage(dependency+"号单车已存在，该单车之后的单车未能成功添加");
            }
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("添加失败");
        }
        return json;
    }

    /**
     *获取单车列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getBikeList")
    public JsonResult getBikeList(Integer pageNum,Integer pageSize){
        JsonResult json = new JsonResult();
        PageHelper.startPage(pageNum,pageSize);
        List<VoBike> list = bikeService.getBikeList();
        Long total = bikeService.getBikeListNum();
        json.setData(new Page(list,total,pageSize,pageNum));
        json.setSuccess(true);
        json.setMessage("获取成功");
        return json;
    }

    /**
     * 根据城市查找单车列表
     * @param pageNum
     * @param pageSize
     * @param voBike
     * @return
     */
    @GetMapping("getBikeByCity")
    public JsonResult getBikeByCity(Integer pageNum,Integer pageSize,VoBike voBike){
        JsonResult json=new JsonResult();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<VoBike> list=bikeService.getBikeByCity(voBike);
            Long total=bikeService.getBikeByCityCount(voBike);
            if (total==0){
                json.setSuccess(false);
                json.setMessage("您所查找的城市不存在，请正确输入城市名称");
            }else {
                json.setData(new Page(list,total,pageSize,pageNum));
                json.setSuccess(true);
                json.setMessage("查找成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("查找失败");
        }
        return json;
    }

    /**
     * 根据商家号查找单车
     * @param pageNum
     * @param pageSize
     * @param voBike
     * @return
     */
    @GetMapping("getBikeByAccount")
    public JsonResult getBikeByAccount(Integer pageNum,Integer pageSize,VoBike voBike){
        JsonResult json=new JsonResult();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<VoBike> list=bikeService.getBikeByAccount(voBike);
            Long total=bikeService.getBikeByAccountCount(voBike);
            if (total==0){
                json.setSuccess(false);
                json.setMessage("您所查找的商家号不存在");
            }else {
                json.setData(new Page(list,total,pageSize,pageNum));
                json.setSuccess(true);
                json.setMessage("查找成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("查找失败");
        }
        return json;
    }

    /**
     * 删除单车
     * @param bicycleCode
     * @return
     */
    @GetMapping("deleteBike")
    public JsonResult deleteBike(String bicycleCode){
        JsonResult json = new JsonResult();
        bikeService.deleteBike(bicycleCode);
        json.setSuccess(true);
        json.setMessage("删除完成");
        return json;
    }
}

